//
//  HomeRecommendGoodsCategoryTabView.swift
//  HomeRecommendGoodsCategoryTabView
//
//  Created by Ben on 2021/8/20.
//

import SwiftUI

struct HomeRecommendGoodsCategoryTabView: View {
    
    @EnvironmentObject var hudState: BenHUDState
    @EnvironmentObject var homeVM: HomeViewModel
    
    @Namespace var animation
    
    var body: some View {
        GeometryReader { proxy -> AnyView in
            
            let minY = proxy.frame(in: .global).minY
            
            let offset = minY - homeVM.homeRecommendMinY
            
            return AnyView(
                
                CategoryListTabView(categoryList: homeVM.myChannelList, currCategoryTab: $homeVM.homeRecommendCurrTab, currCategoryTabIndex: $homeVM.homeRecommendCurrTabIndex, actionViewWidth: 60, actionView: {
                    Button(action: {
                        
                        withAnimation(.easeInOut) {
                            homeVM.showSetChannel.toggle()
                        }
                        
                    }, label: {
                        
                        HStack {
                            
                            Spacer()
                            
                            Image(systemName: "list.dash")
                                .foregroundColor(Color.black.opacity(0.6))
                        }
                        .padding(.trailing, 16)
                        
                    })
                        .frame(width: 60)
                        .frame(maxHeight: .infinity)
                        .background(
                            LinearGradient(
                                colors: [
                                    Color.white.opacity(0.5),
                                    Color.white.opacity(0.7),
                                    Color.white.opacity(0.9),
                                    Color.white.opacity(1),
                                    Color.white.opacity(1),
                                    Color.white.opacity(1)],
                                startPoint: UnitPoint(x: 0, y: 0),
                                endPoint: UnitPoint(x: 1, y: 0))
                        )
                        
                })
                    .background(Color.white)
                    .offset(y: offset < 0 ? -offset : 0)
                
            )
        }
        .frame(maxWidth: .infinity)
        .frame(height: 50)
        .zIndex(10)
        .task {
            if homeVM.myChannelList.count == 0 {
                
                // 设置gif图片，gif后缀名一定要带上
                hudState.wait(message: "正在获取频道信息", gifImageName: "hud_wait.gif")
                
                do {
                    homeVM.myChannelList = try await homeVM.fetchUserChannelList(userId: 1)
                    
                    homeVM.homeRecommendCurrTab = homeVM.myChannelList.first
                    
                    IdleFishUserDefaultsManager.shared.myChannels = homeVM.myChannelList
                    
                    // 成功获取到接口数据后，关闭等待
                    hudState.isWait = false
                    
                    // 接下来模拟网络请求发生错误的情况，触发错误弹窗提示
                    
                    // 修改服务器接口，返回错误信息
                } catch {
                    // cache捕获错误信息
                    
                    if let apiError = error as? APIError {
                        switch apiError {
                        case .custom(let msg):
                            hudState.show(message: msg, systemImageName: "xmark.seal.fill")
                        }
                    }
                    // 重启服务器，让修改生效
                   
                }
            }
        }
    }
}

struct HomeRecommendGoodsCategoryTabView_Previews: PreviewProvider {
    static var previews: some View {
        HomeRecommendGoodsCategoryTabView()
    }
}
struct HomeRecommendGoodsCategoryTabBtn: View {
    
    @EnvironmentObject var homeVM: HomeViewModel
    
    private var tab: HomeRecommendGoodsCategoryModel
    private var selectedTab: HomeRecommendGoodsCategoryModel?
    private var animation: Namespace.ID
    
    private var selectCategoryTabBlock: (_ tab: HomeRecommendGoodsCategoryModel) -> ()
    
    init(tab: HomeRecommendGoodsCategoryModel, selectedTab: HomeRecommendGoodsCategoryModel?, animation: Namespace.ID, selectCategoryTabBlock: @escaping (_ tab: HomeRecommendGoodsCategoryModel) -> ()) {
        self.tab = tab
        self.selectedTab = selectedTab
        self.animation = animation
        self.selectCategoryTabBlock = selectCategoryTabBlock
    }
    
    var body: some View {
        Button(action: {
            
            selectCategoryTabBlock(tab)
            
        }, label: {
            
            ZStack {
                Text(tab.title ?? "")
                    .foregroundColor(Color.black.opacity(selectedTab == tab ? 1 : 0.5))
                    .font(.system(size: 12))
                    .fontWeight(selectedTab == tab ? .bold : .regular)
                    .scaleEffect(selectedTab == tab ? 1.2 : 1)
                
                if selectedTab == tab {
                    Image("home_indicator")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 20)
                        .matchedGeometryEffect(id: HOME_RECOMMEND_CLASSES_TAB_ANIMATION_ID, in: animation)
                        .offset(y: 12)
                } else {
                    Image("home_indicator")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 20)
                        .offset(y: 12)
                        .hidden()
                }
            }
            .padding(.horizontal, 3)
        })
    }
}
